﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Workflow.IDAO;
using Workflow.IService;
using Workflow.Model;

namespace Workflow.Service
{
    public class MenuModelsService : IMenuModelsService
    {
        public IMenuModelsDao MenuModelsDao { get; set; }

        #region IMenuModelsService Members
        public IList<MenuModels> GetAllMenuModels()
        {
            return MenuModelsDao.LoadAll().ToList();
        }
        
        public IList<MenuModels> GetMenuModelsByRole(int rolecode)
        {
            var allList = MenuModelsDao.LoadAll()
                .Where(o => o.Roles != null && o.Roles.Contains("," + rolecode.ToString() + ","))
                .ToList();
            return CreateTree(allList, null);
        }

        private IList<MenuModels> CreateTree(IList<MenuModels> allList, int? parentId)
        {
            var list = allList.Where(o => o.ParentId == parentId).ToList();
            foreach (var l in list)
            {
                l.menus = CreateTree(allList, Convert.ToInt32(l.menuid));
            }
            return list;
        }


        public MenuModels GetMenuModels(int id)
        {
            return MenuModelsDao.Get(id);
        }

        public void SaveMenuModels(MenuModels entity)
        {
            MenuModelsDao.Save(entity);
        }

        #endregion
    }
}
